package componenti.comunication;

import jade.util.leap.Serializable;

import java.util.List;
import java.util.Observable;
import java.util.Vector;

import componenti.buffer.Buffer;
import componenti.buffer.Entity;

@SuppressWarnings({ "rawtypes", "serial" })
public class SimulationFacade extends Observable implements Serializable {
	private Buffer<Entity> buffer;
	private Vector<Vector> produttoreTable, bufferTable, consumatoreTable;

	public SimulationFacade(Buffer<Entity> buffer) {
		super();
		this.buffer = buffer;
		produttoreTable = new Vector<Vector>();
		bufferTable = new Vector<Vector>();
		consumatoreTable = new Vector<Vector>();
	}

	@SuppressWarnings("unchecked")
	public synchronized void notifica(Message c) {
		if (c instanceof Generato) {
			Vector v = new Vector();
			v.add(c.getIDEntity());
			v.add(((double) c.getTime()) / 1000);
			produttoreTable.add(v);
		} else if (c instanceof Prelevato) {
			Vector v = new Vector();
			v.add(c.getIDEntity());
			v.add(((double) c.getTime()) / 1000);
			bufferTable.add(v);
		} else if (c instanceof Processato) {
			Vector v = new Vector();
			v.add(c.getIDEntity());
			v.add(((double) c.getTime()) / 1000);
			consumatoreTable.add(v);
		}
		setChanged();
		notifyObservers(new Packet(produttoreTable, bufferTable,
				consumatoreTable, buffer.numElementi(), false, false));
	}

	public Buffer<Entity> getBuffer() {
		return buffer;
	}

	public void setBuffer(Buffer<Entity> buffer) {
		this.buffer = buffer;
	}

	public void restart() {
		produttoreTable = new Vector<Vector>();
		bufferTable = new Vector<Vector>();
		consumatoreTable = new Vector<Vector>();
		buffer.svuota();
		setChanged();
		notifyObservers(new Packet(null, null, null, 0, true, false));
	}

	public void notificaFineSimulazione() {
		setChanged();
		notifyObservers(new Packet(null, null, null, 0, true, true));
	}

	public class Packet {
		private List<Vector> produttoreTable, bufferTable, consumatoreTable;
		private int size;
		boolean restart;
		boolean simulazioneTerminata;

		public Packet(List<Vector> produttoreTable, List<Vector> bufferTable,
				List<Vector> consumatoreTable, int size, boolean restart,
				boolean simulazioneTerminata) {
			super();
			this.produttoreTable = produttoreTable;
			this.bufferTable = bufferTable;
			this.consumatoreTable = consumatoreTable;
			this.size = size;
			this.restart = restart;
			this.simulazioneTerminata = simulazioneTerminata;
		}

		public List<Vector> getProduttoreTable() {
			return produttoreTable;
		}

		public void setProduttoreTable(List<Vector> produttoreTable) {
			this.produttoreTable = produttoreTable;
		}

		public List<Vector> getBufferTable() {
			return bufferTable;
		}

		public void setBufferTable(List<Vector> bufferTable) {
			this.bufferTable = bufferTable;
		}

		public List<Vector> getConsumatoreTable() {
			return consumatoreTable;
		}

		public void setConsumatoreTable(List<Vector> consumatoreTable) {
			this.consumatoreTable = consumatoreTable;
		}

		public int getSize() {
			return size;
		}

		public void setSize(int size) {
			this.size = size;
		}

		public boolean isRestart() {
			return restart;
		}

		public void setRestart(boolean restart) {
			this.restart = restart;
		}

		public boolean isSimulazioneTerminata() {
			return simulazioneTerminata;
		}

		public void setSimulazioneTerminata(boolean simulazioneTerminata) {
			this.simulazioneTerminata = simulazioneTerminata;
		}

	}

	@Override
	public String toString() {
		return "SimulationFacade [buffer=" + buffer + ", produttoreTable="
				+ produttoreTable + ", bufferTable=" + bufferTable
				+ ", consumatoreTable=" + consumatoreTable + "]";
	}
	
	
}
